package com.ba.base.view

import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.alibaba.android.arouter.launcher.ARouter
import com.ba.base.api.ApiAuth
import com.ba.base.utils.SharedPreUtils
import com.ba.base.utils.ThemeHelper
import com.ba.base.view.dialog.ProgressDialog
import com.ba.base.view.state.IViewState
import com.ba.base.view.status.StatusView
import com.ba.base.view.status.StatusViewBuilder
import qsos.lib.base.base.fragment.BaseFragment

abstract class AbsThemeFragment : BaseFragment(), IViewState {

    var mStateView: StatusView? = null
    var mStateBuilderDef: StatusViewBuilder.Builder? = null
    private var dialog: ProgressDialog? = null
    override val reload: Boolean = false
    val STATE_TOKEN = "token"

    override fun initData(savedInstanceState: Bundle?) {

    }

    override fun getData() {

    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        // 保存token
        outState.putString(STATE_TOKEN, ApiAuth.bearer)
    }

    //恢复数据
    override fun onViewStateRestored(savedInstanceState: Bundle?) {
        super.onViewStateRestored(savedInstanceState)
        //恢复之前输入框的内容
        if (savedInstanceState != null) {
            ApiAuth.bearer = savedInstanceState.getString(STATE_TOKEN, ApiAuth.bearer)
        }
    }
    //恢复数据
    override fun onCreate(bundle: Bundle?) {
        super.onCreate(bundle)
        ApiAuth.bearer = bundle?.getString(STATE_TOKEN, ApiAuth.bearer).toString()
    }
    override fun initView(view: View) {

//        ThemeHelper.mThemeData.observe(this, Observer {
//            notifyTheme(it)
//        })

        mStateBuilderDef = StatusViewBuilder.Builder().setOnEmptyRetryClickListener {
            showLoading()
            getData()
        }.setOnErrorRetryClickListener {
            showLoading()
            getData()
        }.setOnLoginClickListener {
            showError()
            ARouter.getInstance().build("/app/login")
                .navigation()
        }

    }

    override fun onResume() {
        super.onResume()

//        setTheme()

    }
    fun getViewById(@LayoutRes id: Int, parent: ViewGroup? = null): View =
        LayoutInflater.from(mContext).inflate(id, parent, false)

    override fun showEmpty() {
        mStateView?.showEmptyView()
    }

    override fun showError(msg: String?) {
        Handler {
            mStateView?.showErrorView()
            false
        }.sendEmptyMessageDelayed(0, 500)
    }

    override fun showLoading() {
        mStateView?.showLoadingView()
    }

    override fun showSuccess() {
        mStateView?.showContentView()
    }

    override fun showLoginout() {
        mStateView?.showLoginView()
    }

    private fun setTheme() {
        activity?.let {
            val color = SharedPreUtils.getTheme(it)
            val type = EnumTheme.type(color)
            ThemeHelper.mThemeData.postValue(ThemeData(type, type == EnumTheme.GRAY))
        }
    }
    fun showProgress(){
        if (dialog==null){
            dialog = ProgressDialog(requireActivity()).apply {
                setText("加载中...")
            }
        }
        dialog?.show()
    }
    fun dismissProgress(){
        dialog?.dismiss()
    }
}